package dropbox.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
 * this class holds the utils that are common for both server and client. all
 * methods are static in this class
 * 
 * @author Yoav
 * 
 */
public class Utils {
	/**
	 * calculate a file's checkSum
	 * 
	 * @param file a given file
	 * @return the file's checkSum
	 */
	public static String getCheckSum(File file) {
		// holds the file's checkSum
		String checkSum = null;
		
		//holds file's path
		Path path = file.toPath();
		//holds file input stream
		InputStream fis = null;
		try {
			//get file input stream
			fis = Files.newInputStream(path, StandardOpenOption.READ);
			//calculate checkSum
			checkSum = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);

		} catch (java.nio.file.AccessDeniedException e) {

		} catch (IOException e) {

		} finally {
			//close in streams
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
				}
			}
		}
		return checkSum;
	}

}
